<template>
  <div class="add-card">
    <header class="add-header">
      <el-page-header :content="title" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">车辆信息</div>
        <div class="form">
          <el-form label-width="100px" ref="form" :model="form" :rules="rules">
            <el-form-item label="车主姓名" prop="personName">
              <el-input size="small" v-model="form.personName" :disabled="disabled" />
            </el-form-item>
            <el-form-item label="联系方式" prop="phoneNumber">
              <el-input size="small" v-model="form.phoneNumber" :disabled="disabled" />
            </el-form-item>
            <el-form-item label="车辆号码" prop="carNumber">
              <el-input size="small" v-model="form.carNumber" :disabled="disabled" />
            </el-form-item>
            <el-form-item label="车辆品牌" prop="carBrand">
              <el-input size="small" v-model="form.carBrand" :disabled="disabled" />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="form-container">
        <div class="title">{{ form_container_title }}</div>
        <div>
          <el-form label-width="100px" ref="form2" :rules="rules1" :model="form2">
            <el-form-item label="有效日期" prop="cardEndDate">
              <el-date-picker size="small" v-model="form2.cardStartDate" value-format="yyyy-MM-dd" type="date"
                :disabled="disabled" placeholder="开始日期" style="width: 180px;" ></el-date-picker> 至
              <el-date-picker size="small" v-model="form2.cardEndDate" value-format="yyyy-MM-dd" type="date"
                placeholder="结束日期" style="width: 180px;" :picker-options="pickerOptions"
                ></el-date-picker>
            </el-form-item>
            <el-form-item label="支付金额" prop="paymentAmount">
              <el-input size="small" style="width: 51%;" v-model="form2.paymentAmount">
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
            <el-form-item label="支付方式" prop="paymentMethod">
              <el-select size="small" v-model="form2.paymentMethod">
                <el-option label="支付宝" value="alipay"></el-option>
                <el-option label="微信" value="WeChat"></el-option>
                <el-option label="现金" value="Cash"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </div>

      </div>
    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button size="small" @click="clear">重置</el-button>
        <el-button size="small" type="primary" @click="submit">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { addCarCardAPI, rechargeAPI, getCarCardDetail, editCarCard } from '@/api/car'
export default {
  data() {
    return {
      pickerOptions: {
        disabledDate: (date) => {
          const cardStartDate = this.form2.cardStartDate; // 获取开始日期
          return date < cardStartDate; // 如果日期早于开始日期，则禁用
        },
      },

      form_container_title: '最新一次月卡缴费信息',
      disabled: false,
      title: '添加月卡',
      form: {
        personName: '',
        carNumber: '',
        phoneNumber: '',
        carBrand: '',
      },
      form2: {
        cardStartDate: '',
        cardEndDate: '',
        paymentAmount: '',
        cardDate: '',
        paymentMethod: '',
      },
      form3: {
        carInfoId: 0,
        rechargeId: 0
      },
      rules1: {
        cardEndDate: [
          { required: true, message: '时间必须填写', trigger: ['blur', 'change'] },
        ],
        paymentAmount: [
          { required: true, message: '支付金额必须填写', trigger: ['blur', 'change'] },
        ],
        paymentMethod: [
          { required: true, message: '支付方式必须填写', trigger: ['blur', 'change'] },
        ],
      },
      rules: {
        personName: [
          { required: true, message: '车主姓名必须填写', trigger: ['blur', 'change'] },
        ],
        phoneNumber: [
          { required: true, message: '联系方式必须填写', trigger: ['blur', 'change'] },
          { min: 11, max: 11, message: '联系方式必须是11位', trigger: ['blur', 'change'] }
        ],
        carNumber: [
          { required: true, message: '车辆号码必须填写', trigger: ['blur', 'change'] },
          { min: 7, max: 7, message: '车辆号码必须是7位', trigger: ['blur', 'change'] }
        ],
        carBrand: [
          { required: true, message: '车辆品牌必须填写', trigger: ['blur', 'change'] },
        ],
      },
    }
  },
  methods: {
    // changecardDate() {
    //   if (this.form.cardStartDate && this.form.cardEndDate)
    //     this.form.cardDate = [this.form.cardStartDate, this.form.cardEndDate]
    // },
    submit() {
      Promise.all([
        this.$refs.form.validate(),
        this.$refs.form2.validate()
      ]).then(async () => {
        //这时候的then就无所谓传进来的值了，因为只有两个都校验通过才会到这个地方来
        //...
        if (this.$route.query.id) {
          if (this.$route.query.name === '编辑月卡') {
            await editCarCard({
              personName: this.form.personName,
              phoneNumber: this.form.phoneNumber,
              carNumber: this.form.carNumber,
              carBrand: this.form.carBrand,

              cardStartDate: this.form2.cardStartDate,
              cardEndDate: this.form2.cardEndDate,
              paymentAmount: this.form2.paymentAmount,
              paymentMethod: this.form2.paymentMethod,

              carInfoId: this.form3.carInfoId,
              rechargeId: this.form3.rechargeId
            })
            this.$message.success('编辑成功'),
              this.$router.push('/parking/card')
          } else {
            rechargeAPI({
              cardStartDate: this.form2.cardStartDate,
              cardEndDate: this.form2.cardEndDate,
              paymentAmount: this.form2.paymentAmount,
              paymentMethod: this.form2.paymentMethod,
              carInfoId: this.form3.carInfoId,
            }).then(() => {
              this.$message.success('续费成功'),
                this.$router.push('/parking/card')
            }).catch((error) => {
              this.$message.error(error.response.data.msg)
            })
          }

        } else {
          // 增加
          await addCarCardAPI({
            personName: this.form.personName,
            carNumber: this.form.carNumber,
            phoneNumber: this.form.phoneNumber,
            carBrand: this.form.carBrand,

            cardStartDate: this.form2.cardStartDate,
            cardEndDate: this.form2.cardEndDate,
            paymentAmount: this.form2.paymentAmount,
            paymentMethod: this.form2.paymentMethod,
          })
          this.$message.success('添加成功'),
            this.$router.push('/parking/card')
        }
      })
    },
    clear() {
      if(this.$route.query.name != '月卡续费'){
        this.form.personName = '',
        this.form.phoneNumber = '',
        this.form.carNumber = '',
        this.form.carBrand = '' 
        this.form2.cardStartDate = ''
      }
      this.form2.cardEndDate = '',
        this.form2.paymentMethod = '',
        this.form2.paymentAmount = '',

        this.form3.carInfoId = '',
        this.form3.rechargeId = ''
    },
  },
  async mounted() {
    const id = this.$route.query.id
    const name = this.$route.query.name
    if (id) {
      this.title = name
      const res = await getCarCardDetail(id)
      console.log(res)
      this.form.personName = res.data.personName
      this.form.phoneNumber = res.data.phoneNumber
      this.form.carNumber = res.data.carNumber
      this.form.carBrand = res.data.carBrand
      this.form3.carInfoId = res.data.carInfoId
      this.form3.rechargeId = res.data.rechargeId
      this.form2.cardStartDate = res.data.cardStartDate
      this.form2.cardEndDate = res.data.cardEndDate
      // this.form2.cardDate = [, ]
      this.form2.paymentAmount = res.data.paymentAmount
      if (res.data.paymentMethod === 'Alipay')
        this.form2.paymentMethod = '支付宝'
      else if (res.data.paymentMethod === 'WeChat')
        this.form2.paymentMethod = '微信'
      else this.form2.paymentMethod = '线下'

      if (name === '月卡续费') {
        // 月卡已过期，从今天算起
        const tomorrow = new Date();
        tomorrow.setDate(tomorrow.getDate() + 1);
        // 未过期，续费
        const startDate = new Date(res.data.cardEndDate)
        startDate.setDate(startDate.getDate() + 1);
        // 判断赋值“开始续费时间”
        this.form2.cardStartDate = tomorrow > startDate ? tomorrow : startDate
        this.form2.cardEndDate = ''
        this.form2.paymentMethod = ''
        this.form2.paymentAmount = ''
        // 静止修改项
        this.disabled = true
      }
    }
  }
}
</script>

<style scoped lang="scss">
.add-card {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    height: 64px;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }

      .arrow {
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 40px;

    .form-container {
      background-color: #fff;

      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }

      .form {
        border-top: 1px solid #cccccc;
        margin-bottom: 20px;
        padding: 20px 40px 24px;

        .el-form {
          display: flex;
          flex-wrap: wrap;

          .el-form-item {
            width: 50%;
          }
        }
      }
    }

    .preview {
      img {
        width: 100px;
      }
    }
  }

  .add-footer {
    // position: fixed;
    bottom: 0;
    width: 100%;
    padding: 14px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
  }
}
</style>